package org.databasefacade.xml.schema;


import java.io.File;
import java.util.ArrayList;

import org.databasefacade.DatabaseFacade;
import org.databasefacade.LogManager;
import org.databasefacade.registry.TableRegistry;
import org.databasefacade.translator.Translator;
import org.databasefacade.util.XMLDataRecord;
import org.databasefacade.util.XMLDataRecordList;
import org.databasefacade.util.XMLParseUtil;
import org.databasefacade.xml.XMLFileList;
import org.databasefacade.xml.schema.tags.SchemaTagAssign;
import org.databasefacade.xml.schema.tags.TableTagAssign;
import org.databasefacade.xml.schema.tags.ViewTagAssign;
import org.w3c.dom.Document;


public class XMLSchemaLoader implements XMLSchemaConst
{
	XMLFileList schemaFiles;
	XMLParseUtil xmlutil = new XMLParseUtil();
	Document schemaDoc;
	TableRegistry registry;
	
	public XMLSchemaLoader(File file)
	{
		schemaFiles = new XMLFileList(file);
	}
	
	public void load(TableRegistry registry, Translator translator) 
		throws Exception
	{
		this.registry = registry;
		for ( int i = 0; i < schemaFiles.size(); i++ ) {
			parse( schemaFiles.get(i));
		}
	}
	
	public void parse(File file) 
		throws Exception
	{
		schemaDoc = xmlutil.getDocument(file);
		processSchemaAttributes();
		processTables();
		processViews();
	}
	
	public void processSchemaAttributes()
	{
		ArrayList tagHierarchy = new ArrayList();
		tagHierarchy.add(TAG_SCHEMA);
		XMLDataRecordList data = new XMLDataRecordList();
		xmlutil.assignTagXMLData(data, tagHierarchy, schemaDoc);
		LogManager.info(data.toString());
		if ( data.size() > 0 ) {
			XMLDataRecord schema = data.get(0);
			SchemaTagAssign.assign(registry, schema);
		}
	}
	
	private void processTables()
	{     
		ArrayList tagHierarchy = new ArrayList();
		tagHierarchy.add(TAG_SCHEMA);
		tagHierarchy.add(TAG_TABLE);
		XMLDataRecordList tables = new XMLDataRecordList();
		xmlutil.assignTagXMLData(tables, tagHierarchy, schemaDoc);
		LogManager.info(tables.toString());
		if ( tables.size() == 0 ) {
			throw new RuntimeException(
				"There must be atleast one <" + TAG_TABLE + "> element in the schema");
		}
		for ( int i = 0; i < tables.size(); i++ ) {
			XMLDataRecord table = tables.get(i);
			TableTagAssign.assign(registry, table);
		}
	}
	
	private void processViews()
	{     
		ArrayList tagHierarchy = new ArrayList();
		tagHierarchy.add(TAG_SCHEMA);
		tagHierarchy.add(TAG_VIEW);
		XMLDataRecordList views = new XMLDataRecordList();
		xmlutil.assignTagXMLData(views, tagHierarchy, schemaDoc);
		LogManager.info(views.toString());
		for ( int i = 0; i < views.size(); i++ ) {
			XMLDataRecord table = views.get(i);
			ViewTagAssign.assign(registry, table);
		}
	}
	
	

	
	public static void main(String[] args)
		throws Exception
	{
		String fileName = "C:\\dbfacade\\config\\schema\\schema.xml";
		File file = new File(fileName);
		XMLSchemaLoader loader = new XMLSchemaLoader(file);
		DatabaseFacade dbFacade = new DatabaseFacade(DatabaseFacade.ANSI_SQL_92);
		loader.load(dbFacade.getTableRegistry(),
			dbFacade.getDatabaseHelper().getTranslator());
	}
}
